#include "labelscene.h"
// #include "vertexitem.h"
#include <QBrush>
#include <QPen>

VertexItem::VertexItem(QPointF pos, State state, LabelScene *scene, QGraphicsItem *parent)
    : VertexItem(pos, state, scene, 6, parent)
{
    setZValue(10000);
}

VertexItem::VertexItem(QPointF pos, State state, QGraphicsItem *parent)
    : VertexItem(pos, state, nullptr, parent) {}

VertexItem::VertexItem(QPointF pos, QGraphicsItem *parent)
    : VertexItem(pos, VertexItem::Unplaced, parent) {}

VertexItem::VertexItem(QPointF pos, State state,  LabelScene *scene,qreal size ,QGraphicsItem *parent)
    : QGraphicsPathItem(parent)
{
    setSize(size);
    setPos(pos);
    setState(state);

    setAcceptHoverEvents(true);
    setFlag(ItemIsMovable, true);
    setFlag(ItemIsSelectable, true);
    setFlag(ItemSendsGeometryChanges, true);

    if(scene != nullptr)
    {
        myScene = scene;
    }

}
void VertexItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) {
    qDebug() << "hover";
    if(myScene && myScene->mode() == LabelScene::EditPolygon) setState(Hovered);  // 悬停时变为方形
    QGraphicsPathItem::hoverEnterEvent(event);
}

void VertexItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) {
    qDebug() << "live";
    if(myScene && myScene->mode() == LabelScene::EditPolygon) setState(Placed);
    QGraphicsPathItem::hoverLeaveEvent(event);
}

void VertexItem::setState(State state)
{
    myState = state;
    updateStyle();
    update();
}
void VertexItem::updateStyle() {
    switch (myState) {
    case Unplaced:
        updateUnplacedStyle(unplacedPenColor, unplacedBrushColor, unplacedShape);
        break;
    case Placed:
        updatePlaceStyle(placedPenColor, placedBrushColor, placedShape);
        break;
    case Hovered:
        updateHoveredStyle(hoveredPenColor, hoveredBrushColor, hoveredShape);
        break;
    default:
        break;
    }

    setPath(path);
}
void VertexItem::setSize(qreal size)
{
   mySize = size;
}

void VertexItem::mousePressEvent(QGraphicsSceneMouseEvent* event) {

    // QGraphicsPathItem::mousePressEvent(event);  // 确保基类处理选中逻辑
    // qDebug() << "VertexItem clicked!";  // 调试输出
}

QVariant VertexItem::itemChange(GraphicsItemChange change, const QVariant &value)
{
    // if (change == ItemPositionChange && scene()) {
    //     QPointF newPos = value.toPointF();

    //     QRectF rect = myScene->sceneRect();  // 获取场景边界
    //     for(myPolygonItem->vertexes()
    //     // 限制 newPos 在场景内
    //     if (!rect.contains(newPos)) {
    //         return pos();
    //     }
    // }
    return QGraphicsItem::itemChange(change, value);
}
void VertexItem::updateUnplacedStyle(QColor unplacedPenColor, QColor unplacedBrushColor, Shape unplacedShape)
{
    setPen(QPen(unplacedPenColor, penWidth));
    setBrush(unplacedBrushColor);
    updateShape(unplacedShape);
}
void VertexItem::updatePlaceStyle(QColor placedPenColor, QColor placedBrushColor, Shape placedShape)
{

    setPen(QPen(placedPenColor, penWidth));
    setBrush(placedBrushColor);
    updateShape(placedShape);
}

void VertexItem::updateHoveredStyle(QColor hoveredPenColor, QColor hoveredBrushColor, Shape hoveredShape)
{
    setPen(QPen(hoveredPenColor, penWidth));
    setBrush(hoveredBrushColor);
    updateShape(hoveredShape);
}

void VertexItem::updateShape(Shape shape)
{
    if (shape == Circle)
    {
        path = QPainterPath();
        path.addEllipse(-mySize/2, -mySize/2, mySize, mySize);
    } else if (shape == Rectangle)
    {
        path = QPainterPath();
        path.addRect(-mySize/2, -mySize/2, mySize, mySize);
    }
}
